package cn.ztydata.teachingsystem.heaven.web.controller;

import cn.ztydata.teachingsystem.common.Page;
import cn.ztydata.teachingsystem.heaven.cons.MsgTemplate;
import cn.ztydata.teachingsystem.heaven.entity.CourseArrange;
import cn.ztydata.teachingsystem.heaven.service.CourseArrangeService;
import cn.ztydata.teachingsystem.heaven.web.dto.CourseArrangeDto;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultList;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultPage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.MessageFormat;
import java.util.Map;

/**
 * 课程安排控制器
 * Created by wyh on 2015/3/30.
 */
@Controller
@RequestMapping("/lessonPlan")
public class CourseArrangeController extends BaseController {
    @Autowired
    private CourseArrangeService courseArrangeService;

    /**
     * 根据系部编码查询课程下拉框
     * @param dept 系部代码
     * @return Json
     *
     * @author wyh
     * @since 2015-03-30
     */
    @RequestMapping(value = "/lessonCombo", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultList<Map<String, Object>> getCourseList(@RequestParam("departCode") String dept){
        Assert.isTrue(!StringUtils.isEmpty(dept), MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "系部代码"));

        log.debug("返回 课程信息");
        return new JsonResultList<>(courseArrangeService.getCourseList(dept));
    }

    /**
     * 课程安排列表
     * @param depart 系部代码
     * @param courseId 课程ID
     * @param startYear 起始学年
     * @param term 学期 0上1下2全部
     * @param page 页码
     * @param pageSize  页数
     * @return json
     *
     * @author wyh
     * @since 2015-03-31
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultPage<CourseArrangeDto> listArrange(@RequestParam(value = "departCode", required = false) String depart,
                                                   @RequestParam(value = "lessonId", required = false) long courseId,
                                                 @RequestParam("startYear") String startYear,
                                                 @RequestParam("term") int term,
                                                 @RequestParam("page") int page,
                                                 @RequestParam("pageSize") int pageSize) {
        //校验
        Assert.notNull(startYear, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "起始学年"));
        Assert.isTrue(term >= 0 && term <= 2, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "学期"));
        Assert.isTrue(page > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "当前页码"));
        Assert.isTrue(pageSize > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "每页条数"));

        //调整数据
        if(startYear.equals("0")) {
            log.debug("学年为0：startYear.equals(\"0\")");
            startYear = "";
        }

        Page<CourseArrange> arrangePage = courseArrangeService.listArrange(depart, courseId, startYear, term, page, pageSize);

        log.debug("返回 课程安排列表");
        return new JsonResultPage<>(new Page<>(arrangePage.getTotal(), arrangePage.getSize(), arrangePage.getNum(), mapper.mapList(arrangePage.getItems(), CourseArrangeDto.class)));
    }

    /**
     * 学年下拉框
     * @return json
     *
     * @author wyh
     * @since 2015-4-16
     */
    @RequestMapping(value = "/startYearCombo", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultList<String> getYearList(){
        log.debug("返回 所有学年");
        return new JsonResultList<>(courseArrangeService.getYearList());
    }
}
